package com.xiaochun.jiankangapp.extension

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.xiaochun.jiankangapp.R
import com.xiaochun.jiankangapp.base.AppContext


fun View.onClick(click: OnClickListener?) {
    this.setOnClickListener(click)
}

/**
 * Fragment 和 Activity 可以使用此方法获取自己对应的Model
 * 获取一个ViewModel ，懒加载
 */
inline fun <reified T : ViewModel> lazyGlobalViewModel(
    key: String? = null,
    factory: ViewModelProvider.Factory? = null
): Lazy<T> {
    val f = if (AndroidViewModel::class.java.isAssignableFrom(T::class.java)) {
        ViewModelProvider.AndroidViewModelFactory(AppContext.globalContext())
    } else {
        factory
    }
    return lazy(LazyThreadSafetyMode.NONE) {
        ViewModelProvider(AppContext.globalContext() as ViewModelStoreOwner).get(T::class.java)
    }
}

fun showToast(char: CharSequence? = null) {
    char ?: return
    AppContext.globalContext().let {
        Toast.makeText(it, char, Toast.LENGTH_SHORT).show()
    }
}

fun ImageView.loadAvatar(url: Any?) {
    Glide.with(context).load(url).apply {
        transform(CenterCrop(), CircleCrop())
    }.placeholder(R.mipmap.icon_logo).into(this)
}

fun ImageView.loadUrl(url: Any?, circle: Boolean = false) {
    Glide.with(context).load(url).apply {
        if (circle) {
            transform(CenterCrop(), CircleCrop())
        } else {
            transform(CenterCrop())
        }
    }.into(this)
}

fun ImageView.loadUrl(url: Any?, vararg transformations: BitmapTransformation) {
    loadUrl(url, -1, *transformations)
}

fun ImageView.loadUrl(
    url: Any?,
    placeholder: Int = -1,
    vararg transformations: BitmapTransformation
) {
    Glide.with(context).load(url).apply {
        if (transformations.isNotEmpty()) {
            transform(CenterCrop(), *transformations)
        } else {
            transform(CenterCrop())
        }
        if (placeholder != -1) {
            placeholder(placeholder)
        }
    }.into(this)
}

fun Context.start(clazz: Class<*>, bundle: Bundle? = null) {
    startActivity(Intent(this, clazz).apply {
        if (bundle != null) {
            putExtras(bundle)
        }
    })
}

fun Fragment.start(clazz: Class<*>, bundle: Bundle? = null) {
    startActivity(Intent(requireContext(), clazz).apply {
        if (bundle != null) {
            putExtras(bundle)
        }
    })
}

